package com.yy.job.core.domain;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.enums.JobStepStatusEnum;

/**
 * 任务步骤建模域
 * 
 * @author eleven.li
 *
 */
public class JobStepDomain extends BaseDomain {

	private static final long serialVersionUID = 9019463818216705095L;

	private Long jobId;
	
	private String stepCode;

	private String stepName;

	private JobStepStatusEnum stepStatus;

	private String stepParams;

	private Integer stepOrder;
	
	private Integer execCount;
	
	private Date execTime;

	@JSONField(serialize = false)
	private JobDomain jobDomain;

	public JobStepDomain() {
	}

	public JobStepDomain(YYJobStepDO jobStepDO) {
		setId(jobStepDO.getId());
		setJobId(jobStepDO.getJobId());
		setStepCode(jobStepDO.getStepCode());
		setStepName(jobStepDO.getStepName());
		
		if (StringUtils.isNotBlank(jobStepDO.getStepStatus())) {
			setStepStatus(JobStepStatusEnum.findByCode(jobStepDO.getStepStatus()));
		}
		setStepParams(jobStepDO.getStepParams());
		setStepOrder(jobStepDO.getStepOrder());
		this.setExecCount(jobStepDO.getExecCount());
		this.setExecTime(jobStepDO.getExecTime());
		
		setOperator(jobStepDO.getModifier());
	}

	public YYJobStepDO convertToDO() {
		YYJobStepDO jobStepDO = new YYJobStepDO();
		jobStepDO.setId(getId());
		jobStepDO.setJobId(this.jobId);
		jobStepDO.setStepCode(this.stepCode);
		jobStepDO.setStepName(this.stepName);
		if (this.stepStatus != null) {
			jobStepDO.setStepStatus(this.stepStatus.getCode());
		}
		jobStepDO.setStepParams(this.stepParams);
		jobStepDO.setStepOrder(this.stepOrder);
		jobStepDO.setExecTime(this.execTime);
		jobStepDO.setExecCount(this.execCount);
		jobStepDO.setCreator(getOperator());
		jobStepDO.setModifier(getOperator());
		if (getIsDeleted() != null) {
			jobStepDO.setIsDeleted(getIsDeleted().getCode());
		}
		return jobStepDO;
	}

	public String toString() {
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("id", getId());
		jsonObj.put("stepCode", this.stepCode);
		jsonObj.put("stepStatus", this.stepStatus);
		jsonObj.put("jobId", this.jobId);
		jsonObj.put("stepOrder", this.stepOrder);
		return jsonObj.toJSONString();
	}

	public static String taskListToString(List<JobStepDomain> taskList) {
		if (taskList == null)
			return "NULL";
		if (taskList.size() == 0)
			return "empty task list";
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("[");
		for (JobStepDomain task : taskList) {
			strBuf.append(task.toString());
			strBuf.append(",");
		}
		return strBuf.toString().substring(0, strBuf.length() - 1) + "]";
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public String getStepCode() {
		return stepCode;
	}

	public void setStepCode(String stepCode) {
		this.stepCode = stepCode;
	}

	public String getStepName() {
		return stepName;
	}

	public void setStepName(String stepName) {
		this.stepName = stepName;
	}

	public JobStepStatusEnum getStepStatus() {
		return stepStatus;
	}

	public void setStepStatus(JobStepStatusEnum stepStatus) {
		this.stepStatus = stepStatus;
	}

	public String getStepParams() {
		return stepParams;
	}

	public void setStepParams(String stepParams) {
		this.stepParams = stepParams;
	}

	public Integer getStepOrder() {
		return stepOrder;
	}

	public void setStepOrder(Integer stepOrder) {
		this.stepOrder = stepOrder;
	}

	public JobDomain getJobDomain() {
		return jobDomain;
	}

	public void setJobDomain(JobDomain jobDomain) {
		this.jobDomain = jobDomain;
	}

	public Integer getExecCount() {
		return execCount;
	}

	public void setExecCount(Integer execCount) {
		this.execCount = execCount;
	}

	public Date getExecTime() {
		return execTime;
	}

	public void setExecTime(Date execTime) {
		this.execTime = execTime;
	}

}
